﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using XMLGener.Utils;
using XMLGener.Data;
namespace XBasicModules
{
    /// <summary>
    /// Base of module generators
    /// </summary>
    public class ModuleBase
    {

        private XmlElement settingsElement = null;

        /// <summary>
        /// Restriction, that are set for generating and must be accepted
        /// </summary>
        protected XDataType restrictions = null;
        
        /// <summary>
        /// This is reference to logger. 
        /// It is set in method checkSettingsElement.
        /// </summary>
        protected XLogger logger = null;
        
        /// <summary>
        /// Check, if input string is xml setting.
        /// </summary>
        /// <param name="settingsXml">xml data in string</param>
        /// <param name="parentDocument">parent document, which method can be used</param>
        /// <returns>true, if input string is xml setting</returns>
        protected bool checkSettingsElement(string settingsXml, XMLGenerDocument parentDocument)
        {
            XmlDocument doc = new XmlDocument();
            // set logger
            if (parentDocument!=null)
                this.logger = parentDocument.logger;
            // set default logger
            if (logger == null)
                logger = XLogger.instance;

            try
            {
                doc.LoadXml(settingsXml);
            }
            catch (XmlException )
            {
                logger.addErrorLog(this.GetType().ToString(), "Error in parsing settings:" + settingsXml);
                return false;
            }

            XmlElement mainElement = doc.DocumentElement;
            if (mainElement == null)
            {
                logger.addErrorLog(this.GetType().ToString(), "Settings Xml is possibly empty:" + settingsXml);
                return false;
            }

            settingsElement = XMLUtils.getSubElement(mainElement, "setting");
            if (settingsElement == null)
            {
                logger.addErrorLog(this.GetType().ToString(), "Settings Xml is possibly empty:" + settingsXml);
                return false;
            }

            XmlElement restrictionElement = XMLUtils.getSubElement(mainElement, "restriction");
            if (restrictionElement != null)
            {
                restrictions = new XDataType(restrictionElement);
            }
            return true;
        }

        /// <summary>
        /// Return attribute from settings element.
        /// </summary>
        /// <param name="name">attribute name</param>
        /// <returns>value of attrivbute. Return null if attribute was not found.</returns>
        protected string getParam(string name)
        {
            if (this.settingsElement.HasAttribute(name))
            {
                return this.settingsElement.GetAttribute(name);
            }
            return null;
        }

    }
}
